/**
 * Created by Thyiad on 2017/11/13.
 */
import Vue from 'vue';
import Vuex from 'vuex';
import createLogger from 'vuex/dist/logger';
import * as actionTypes from '../constants/action-types';
import * as mutationTypes from '../constants/mutation-types';
import auth from '../utils/auth';
import utils from '../utils/utils';
import * as api from '../constants/api';

Vue.use(Vuex);

const debug = process.env.NODE_ENV !== 'production'

// initial state
const state={
  userinfo: null,
  companyInfo: null,
  creditInfo: null,
  carList:null,
  driverList:null,
  checkList:null,
  orderList:null,
  msgList:null,
}

// getters
const getters={

}

// mutations
const mutations={
  [mutationTypes.SET_USERINFO](state, userinfo){
    state.userinfo=userinfo
  },
  [mutationTypes.SET_COMPANYINFO](state, companyInfo){
    state.companyInfo=companyInfo
  },
  [mutationTypes.SET_CARLIST](state, carList){
    state.carList=carList
  },
  [mutationTypes.SET_DRIVERLIST](state, driverList){
    state.driverList=driverList
  },
  [mutationTypes.SET_CREDITINFO](state, creditInfo){
    state.creditInfo=creditInfo
  },
  [mutationTypes.SET_CHECKLIST](state, checkList){
    state.checkList=checkList
  },
  [mutationTypes.SET_ORDERLIST](state, orderList){
    state.orderList=orderList
  },
  [mutationTypes.SET_MSGLIST](state, msgList){
    state.msgList=msgList
  }
}

// actions
const actions={
  [actionTypes.LOGIN]({commit}, userinfo){
    return new Promise((resolve, reject) => {
      utils.post(api.LOGIN, userinfo).then(data=>{
        console.log(`login success: ${data}`)
        auth.login(data)
        resolve()
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.REGISTER]({commit}, userinfo){
    return new Promise((resolve, reject) => {
      utils.post(api.REGISTER, userinfo).then(data=>{
        console.log(`register success: ${data}`)
        resolve()
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.LOGOUT]({commit}){
    return new Promise((resolve, reject)=>{
      auth.logout();
      resolve()
    })
  },
  [actionTypes.GET_COMPANYINFO]({commit}){
    return new Promise((resolve, reject)=>{
      utils.post(api.GET_COMPANYINFO).then(data=>{
        commit(mutationTypes.SET_COMPANYINFO, data);
        resolve()
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.EDIT_COMPANYINFO]({commit}, newCompanyInfo){
    return new Promise((resolve, reject)=>{
      utils.post(api.EDIT_COMPANYINFO, newCompanyInfo).then(data=>{
        commit(mutationTypes.SET_COMPANYINFO, data);
        resolve()
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.GET_CARLIST]({commit}){
    return new Promise((resolve, reject)=>{
      utils.post(api.GET_CARLIST).then(data=>{
        commit(mutationTypes.SET_CARLIST, data);
        resolve();
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.ADD_CAR]({commit, state}, car){
    return new Promise((resolve, reject) => {
      utils.post(api.ADD_CAR, car).then(data => {
        let carList = [...state.carList];
        carList.push(data);
        commit(mutationTypes.SET_CARLIST, carList);
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  [actionTypes.EDIT_CAR]({commit, state}, car) {
    return new Promise((resolve, reject) => {
      utils.post(api.EDIT_CAR, car).then(data => {
        let carList = [...state.carList];
        let findedCar = carList.find(function (item) {
          return item.id === car.id;
        })
        if (findedCar) {
          let editCarIndex = carList.indexOf(findedCar);
          if (editCarIndex >= 0) {
            carList[editCarIndex] = data;
            commit(mutationTypes.SET_CARLIST, carList);
          }
        }
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  [actionTypes.DELETE_CAR]({commit, state}, id){
    return new Promise((resolve, reject) => {
      utils.post(api.DELETE_CAR,{id: id}).then(data => {
        let carList = [...state.carList];
        let findedCar = carList.find(function (item) {
          return item.id === id;
        })
        if (findedCar) {
          let deleteCarIndex = carList.indexOf(findedCar);
          if (deleteCarIndex >= 0) {
            carList.splice(deleteCarIndex, 1);
            commit(mutationTypes.SET_CARLIST, carList);
          }
        }
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  [actionTypes.GET_CREDITINFO]({commit}){
    return new Promise((resolve, reject)=>{
      utils.post(api.GET_CREDITINFO).then(data=>{
        commit(mutationTypes.SET_CREDITINFO, data);
        resolve();
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.GET_DRIVERLIST]({commit}){
    return new Promise((resolve, reject)=>{
      utils.post(api.GET_DRIVERLIST).then(data=>{
        commit(mutationTypes.SET_DRIVERLIST, data);
        resolve();
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.ADD_DRIVER]({commit, state}, driver){
    return new Promise((resolve, reject) => {
      utils.post(api.ADD_DRIVER, driver).then(data => {
        let driverList = [...state.driverList];
        driverList.push(data);
        commit(mutationTypes.SET_DRIVERLIST, driverList);
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  [actionTypes.EDIT_DRIVER]({commit, state}, driver) {
    return new Promise((resolve, reject) => {
      utils.post(api.EDIT_DRIVER, driver).then(data => {
        let driverList = [...state.driverList];
        let findedDriver = driverList.find(function (item) {
          return item.id === driver.id;
        })
        if (findedDriver) {
          let editDriverIndex = driverList.indexOf(findedDriver);
          if (editDriverIndex >= 0) {
            driverList[editDriverIndex] = data;
            commit(mutationTypes.SET_DRIVERLIST, driverList);
          }
        }
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  [actionTypes.DELETE_DRIVER]({commit, state}, id){
    return new Promise((resolve, reject) => {
      utils.post(api.DELETE_DRIVER,{id: id}).then(data => {
        let driverList = [...state.driverList];
        let findedCar = driverList.find(function (item) {
          return item.id === id;
        })
        if (findedCar) {
          let deleteDriverIndex = driverList.indexOf(findedCar);
          if (deleteDriverIndex >= 0) {
            driverList.splice(deleteDriverIndex, 1);
            commit(mutationTypes.SET_DRIVERLIST, driverList);
          }
        }
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  [actionTypes.GET_ORDERLIST]({commit}){
    return new Promise((resolve, reject)=>{
      utils.post(api.GET_ORDERLIST).then(data=>{
        commit(mutationTypes.SET_ORDERLIST, data);
        resolve();
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.GET_CHECKLIST]({commit}){
    return new Promise((resolve, reject)=>{
      utils.post(api.GET_CHECKLIST).then(data=>{
        commit(mutationTypes.SET_CHECKLIST, data);
        resolve()
      }).catch(error=>{
        reject(error)
      })
    })
  },
  [actionTypes.GET_MSG]({commit}){
    return new Promise((resolve, reject)=>{
      utils.post(api.GET_MSG).then(data=>{
        commit(mutationTypes.SET_MSGLIST, data);
        resolve()
      }).catch(error=>{
        reject(error)
      })
    })
  }
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
  strict: debug,
  plugins: debug?[createLogger()]:[],
})
